<?php
namespace app\index\controller;
use think\Db;
use think\Validate;

/**
 * Class User
 * @package app\index\controller
 * 个人中心
 */
class User extends Base{
    /**
     * 获取签到明细
     * @author zheng
     */
    public function getSign(){

        $dat = input();

        $user = $this -> vyUser($dat);//身份验证

        if(isset($user['status'])){
            return json($user);
        }

        //$startime = date('Y-m-d H:i:s',strtotime('-29 days'));
        $endtime = date('Y-m-01 00:00:00');

        $sel_sign = Db::name('city_sign')
            ->where(['user_id' => $user['user_id']])
            ->where('sign_time','> time',$endtime)
            //->where('sign_time','between time',[$startime,$endtime])
            ->select();

        $sign = Db::name('city_integral')
            ->where(['user_id'=>$user['user_id']])
            ->find();

        return json(['status'=>200,'msg'=>'拉取成功','sign'=>$sign,'data'=>$sel_sign]);
    }

    /**
     * 个人签到
     * @author zheng
     */
    public function signIn(){
        $dat = input();

        $user = $this -> vyUser($dat);//身份验证

        if(isset($user['status'])){
            return json($user);
        }

        $today = date('Y-m-d H:i:s');

        $add_data = array(
            'user_id' => $user['user_id'],
            'sign_time' => $today,
            'sign_date' => date('d'),
          //  'this_time_sign' => $this->getSignVal()
        );

        $sel_sign = Db::name('city_sign')
            ->where(['user_id'=>$user['user_id']])
            ->order('sign_time desc')
            ->find();

        if(empty($sel_sign) || date('Y-m-d 06:00:00')>$sel_sign['sign_time']){

            Db::startTrans();
            try{
                $add_sign = Db::name('city_sign')->insert($add_data);

                $up_user_sign = Db::name('city_integral')
                    ->where(['user_id'=>$user['user_id']])
                    ->setInc('total_integral', $this->getSignVal());

                Db::commit();
            }
            catch(\Exception $e){
                Db::rollback();
                return json(['status'=>0,'msg'=>'签到失败，请联系管理员']);
            }


        }else{
                return json(['status'=>0,'msg'=>'今天已经签到，请明天继续保持签到']);
        }
            $sel_sign = Db::name('city_integral')
                ->where(['user_id'=>$user['user_id']])
                ->find();

            $sign['sign'] = $sel_sign;

            return json(['status'=>200,'msg'=>'签到成功，明天再接再厉','data'=>$sign]);

    }

    /**
     * 积分明细
     * @author zheng
     */
    public function integraList(){
        $dat = input();
        $dat['pageSize'] =isset($dat['pageSize'])? $dat['pageSize']:1;
        $dat['pageNum'] = isset($dat['pageNum'])? $dat['pageNum']:15;
        $user = $this -> vyUser($dat);//身份验证

        if(isset($user['status'])){
            return json($user);
        }

        $sel_sign = Db::name('city_integral')
            ->where(['user_id' => $user['user_id']])
            ->find();

        $sel_all_sign = Db::name('city_integral_detail')
            ->where(['user_id'=>$user['user_id']])
            ->page( $dat['pageSize'],$dat['pageNum'])
            ->select();

        return json(['status'=>200,'msg'=>'拉取成功','dat'=>$sel_sign,'data'=>$sel_all_sign]);
    }

    /**
     * 拉取公告列表
     * @author zheng
     */
    public function getAnt(){
        $dat = input();

        $user = $this -> vyUser($dat);//身份验证

        if(isset($user['status'])){
            return json($user);
        }

        $dat['page'] =isset($dat['page'])? $dat['page']:1;
        $dat['limit'] = isset($dat['limit'])? $dat['limit']:100;

        $sel_notice = Db::name('city_notice')
            ->where(['show'=>1])
           -> where('create_time','> time',$user['create_time'])
           // ->where("create_time > {$user['create_time']}")
            ->page($dat['page'],$dat['limit'])
            ->select();

        return json(['status'=>200,'msg'=>'拉取成功','data'=>$sel_notice]);

    }

    /**
     * 拉取公告详情
     */
    public function getAntDet(){
        $dat = input();

        $user = $this -> vyUser($dat);//身份验证
        if(isset($user['status'])){
            return json($user);
        }

        $validate = new Validate([
            "id" => "require",
        ],
            [
                "id.require" => "公告id不能为空",
            ]
        );

        if(!$validate->check($dat)){
            return json(['status'=>0,'msg'=>$validate->getError()]);
        }

        $sel_notice  = Db::name('city_notice')
            ->where(['id'=>$dat['id']])
            ->find();

        return json(['status'=>200,'msg'=>'拉取成功','data'=>$sel_notice]);

    }
    /**
     * 获取个人信息
     * @author zheng
     */

    public function getUserMsg()
    {
        $dat = input();

        $user = $this->vyUser($dat);//身份验证
        if (isset($user['status'])) {
            return json($user);
        }

        return json(['status'=>200,'msg'=>'拉取成功','data'=>$user]);

    }

    /**
     * 修改个人信息
     */
    public function upUserMsg(){
        $dat = input();

        $user = $this->vyUser($dat);//身份验证
        if (isset($user['status'])) {
            return json($user);
        }

        $vy = new Validate([
          //  'mobile' =>'/^1[2345789]\d{9}$/',
          //  'nick_name'=>'require',
            'company'=>'require',
            'duty'=>'require',
            'address'=>'require',
            'signature'=>'require',
            'card_style'=>'require',

        ],
            [
             //   'mobile' =>'电话格式不正确',
             //   'nick_name.require' =>'昵称不能为空',
                'company.require' =>'公司不能为空',
                'duty.require' =>'职务不能为空',
                'address.require' =>'地址不能为空',
                'signature.require' =>'电话格式不正确',
                'card_style.require' =>'名片风格不能为空'

            ]
        );

        if(!$vy->check($dat)){
            return json(['status'=>0,'msg'=>$vy->getError()]);
        }

        $up_data = array(
         //   'mobile' =>$dat['mobile'],
        //    'nick_name'=>$dat['nick_name'],
            'company'=>$dat['company'],
            'duty'=>$dat['duty'],
            'address'=>$dat['address'],
            'signature'=>'require',
            'card_style'=>$dat['card_style'],
        );

        $up_user = Db::name('city_user')
            ->where(['user_id'=>$user['user_id']])
            ->update($up_data);

        if($up_user === false){
            return json(['status'=>0,'msg'=>'修改失败']);
        }

        $sel_user = Db::name('city_user')
            ->where(['user_id'=>$user['user_id']])
            ->find();

        return json(['status'=>200,'msg'=>'修改成功','data'=>$sel_user]);

    }

    /**
     * 获取个人关注
     */

    public function getAttention(){
        $dat = input();

        $user = $this->vyUser($dat);//身份验证
        if (isset($user['status'])) {
            return json($user);
        }

        $vy = new Validate([
            //  'mobile' =>'/^1[2345789]\d{9}$/',
            //  'nick_name'=>'require',
            'infoid'=>'require',
            'operationType'=>'require'


        ],
            [
                //   'mobile' =>'电话格式不正确',
                //   'nick_name.require' =>'昵称不能为空',
                'infoid.require' =>'商户Id不能为空',
                'operationType.require' =>'类型不能为空'
            ]
        );

        if(!$vy->check($dat)){
            return json(['status'=>0,'msg'=>$vy->getError()]);
        }

        $sel_track = Db::name('city_track')
            ->where(['user_id'=>$user['user_id'],'info_id'=>$dat['infoid'],'operation_type'=>$dat['operationType']])
            ->select();

         return json(['status'=>200,'msg'=>'拉取成功','data'=>$sel_track]);

    }

    /*
 * 获取分类信息列表
 */
    public function getUserClassList(){

        $dat = input();
        $where = "";
        if(isset($dat['rd_session'])){
            $user = $this->vyUser($dat);//身份验证
            if (isset($user['status'])) {
                return json($user);
            }

            $where['user.user_id'] =$user['user_id'];
        }

        $dat['pageSize'] =isset($dat['pageSize'])? $dat['pageSize']:1;
        $dat['pageNum'] = isset($dat['pageNum'])? $dat['pageNum']:15;

        $sel_list = Db::name('city_user')
            ->alias('user')
            ->join('t_city_info info','user.user_id = info.user_id')
            ->join('t_city_info_imgs img','info.id = img.info_id')
            ->where($where)
            ->select();

        return json(['status'=>200,'msg'=>'拉取成功','data'=>$sel_list]);

    }

}